package com.vt.common.security.util.parser;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vt.common.core.util.R;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @Auther: panZha
 * @Date: 2018/11/22 10:21
 * @Description:
 */
public abstract class ResultDataParser {

    public abstract void parse(Object data);
    abstract Field getIdField(Field f, Field[] fs);


    protected Collection getResData(Object data) {
        Collection cos;

        if (data instanceof R) {
            data = ((R) data).getData();
        }

        if (null == data)
            return null;

        if (data instanceof Collection) {
            cos = (Collection) data;
        } else if (data instanceof IPage) {
            cos = ((IPage) data).getRecords();
        } else {
            cos = new ArrayList();
            cos.add(data);
        }
        return cos;
    }

    protected String removeSuffix(String str, String... suffix) {
        String newStr = str;

        for (String s : suffix) {
            newStr = StrUtil.removeSuffix(newStr, s);
        }
        return newStr;
    }
}
